#include<iostream>
#include<vector>
using namespace std;


int search(vector<int>& nums, int target) {
    int minIndex = 0;
    int maxIndex = nums.size() - 1;
    int firstPosition = 0;
    //寻找第一个小于nums[0]的元素
    while(minIndex <= maxIndex)
    {
        int midIndex = ((maxIndex - minIndex)>>1) + minIndex;//2,0,
        if(nums[midIndex] <= nums[0])
        {
            firstPosition = midIndex;
            maxIndex = midIndex - 1;
        }else
        {
            minIndex = midIndex + 1;
        }
    }
    cout<<"firstPosition:"<<firstPosition<<endl;
    minIndex = 0;
    maxIndex = nums.size() - 1;
    if(target == nums[maxIndex])
    {
        return maxIndex;
    }else if(target > nums[maxIndex])
    {
        maxIndex = firstPosition-1;
    }else{
        minIndex = firstPosition;
    }
    while(minIndex <= maxIndex)
    {
        int midIndex = ((maxIndex - minIndex)>>1) + minIndex;
        if(nums[midIndex] == target)
        {
            return midIndex;
        }else if(nums[midIndex] < target)
        {
            minIndex = midIndex + 1;
        }
        else{
            maxIndex = midIndex - 1;
        }
    }
    return -1;
}
int main()
{
    vector<int> nums(5);
    int target;
    cout << "Enter nums: "<<endl;
    for(int i = 0; i < nums.size(); i++)
    {
        cin >> nums[i];
    }
    cout<<"enter number:"<<endl;
    cin >> target;
    int result = search(nums, target);
    cout<<"result:"<<result;
    return 0;
}